/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nts.pointsstore;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Himagirinathan
 */
public class ConfirmChecked extends Activity implements View.OnClickListener
{
  ArrayList<String> checkeditems;
  private DataHelper dh;
  List<String[]> names;
  private int totalpts,totalprice;
  Button bcheckout;
  long id;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        checkeditems=this.getIntent().getExtras().getStringArrayList("checked");
        id=this.getIntent().getExtras().getLong("id");
        dh = new DataHelper(this);
        names = this.dh.selectAllProducts();
        ScrollView sv=new ScrollView(this);
        TableLayout tl=new TableLayout(this);
        tl.setColumnStretchable(0, true);
        TableRow row = null;
        TextView tvlabel = null,tvpoints=null,tvrate=null;
        CheckBox cb=null;
        row=new TableRow(this);
        row.setPadding(0, 15, 0, 15);
        tvlabel=new TextView(this);
        tvlabel.setText("Product");
        tvlabel.setTextSize(20.0f);
        tvlabel.setPadding(10, 0, 15, 0);
        tvpoints=new TextView(this);
        tvpoints.setText("Points");
        tvpoints.setTextSize(20.0f);
        tvpoints.setGravity(Gravity.RIGHT);
        tvpoints.setPadding(0, 0, 15, 0);
        tvrate=new TextView(this);
        tvrate.setText("Rate");
        tvrate.setTextSize(20.0f);
        tvrate.setGravity(Gravity.RIGHT);
        tvrate.setPadding(0, 0, 15, 0);
        row.addView(tvlabel);
        row.addView(tvpoints);
        row.addView(tvrate);
        tl.addView(row);
        totalpts=0;
        totalprice=0;
        for(String name[]:names)
        {
            for(int i=0;i<checkeditems.size();i++)
            {
             String itstr=checkeditems.get(i);
             String arrstr=name[0];
             if(itstr.equals(arrstr))
             {
              row=new TableRow(this);
              tvlabel=new TextView(this);
              tvlabel.setText(name[1]);
              tvlabel.setTextSize(16.0f);
              tvlabel.setId(Integer.parseInt(name[0]));
              tvlabel.setPadding(10, 0, 15, 0);
              tvpoints=new TextView(this);
              tvpoints.setText(name[2]);
              tvpoints.setTextSize(16.0f);
              tvpoints.setGravity(Gravity.RIGHT);
              totalpts+=Integer.parseInt(name[2]);
              tvpoints.setId(Integer.parseInt(name[0]));
              tvpoints.setPadding(0, 0, 15, 0);
              tvrate=new TextView(this);
              tvrate.setText(name[3]+"$");
              tvrate.setGravity(Gravity.RIGHT);
              tvrate.setTextSize(16.0f);
              totalprice+=Integer.parseInt(name[3]);
              tvrate.setId(Integer.parseInt(name[0]));
              tvrate.setPadding(0, 0, 15, 0);
              cb=new CheckBox(this);
              cb.setId(Integer.parseInt(name[0]));
              cb.setChecked(true);
              cb.setOnClickListener(this);
              row.addView(tvlabel);
              row.addView(tvpoints);
              row.addView(tvrate);
              row.addView(cb);
              tl.addView(row);
              break;
             }
            }
          }
          row=new TableRow(this);
          row.setPadding(0, 20, 0, 0);
          tvlabel=new TextView(this);
          tvlabel.setText("Total");
          tvlabel.setTextSize(20.0f);
          tvlabel.setPadding(10, 0, 15, 0);
          tvpoints=new TextView(this);
          tvpoints.setText(""+totalpts);
          tvpoints.setTextSize(20.0f);
          tvpoints.setGravity(Gravity.RIGHT);
          tvpoints.setPadding(0, 0, 15, 0);
          tvrate=new TextView(this);
          tvrate.setText(totalprice+"$");
          tvrate.setTextSize(20.0f);
          tvrate.setGravity(Gravity.RIGHT);
          tvrate.setPadding(0, 0, 15, 0);
          row.addView(tvlabel);
          row.addView(tvpoints);
          row.addView(tvrate);
          tl.addView(row);
          row=new TableRow(this);
          row.setPadding(0, 20, 0, 0);
          row.setGravity(Gravity.CENTER_HORIZONTAL);
          bcheckout=new Button(this);
          bcheckout.setText("Check Out");
          bcheckout.setOnClickListener(this);
          row.addView(bcheckout);
          tl.addView(row);
          sv.addView(tl);
          this.setContentView(sv);
    }

    public void onClick(View arg0)
    {
     if(arg0 instanceof CheckBox)
     {
       CheckBox cbc=(CheckBox)arg0;
       if (cbc.isChecked())
        checkeditems.add(""+cbc.getId());
       else
        checkeditems.remove(""+cbc.getId());
     }
     else if(arg0 instanceof Button)
     {
      if(checkeditems.size()==0)
      {
       AlertDialog.Builder ad=new AlertDialog.Builder(this);
       ad.setMessage("No Product Selected");
       ad.setNeutralButton("Ok", null);
       ad.show();
      }
      else
      {
       /*
       AlertDialog.Builder ad=new AlertDialog.Builder(this);
       ad.setMessage(checkeditems.size()+" Selected");
       ad.setNeutralButton("Ok", null);
       ad.show();
       */
       
        Intent it=new Intent(this, Summary.class);
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("checked", checkeditems);
        bundle.putLong("id",id);
        it.putExtras(bundle);
        this.startActivity(it);
      }
     }
    }

}
